package utils

import (
	"fmt"
	"os"
	"os/exec"
	"runtime/debug"
)

// OutputMsg append messages to [target] file using Linux 'echo' command,
// would create the [target] file if not existed.
func OutputMsg(msg, target string) {
	fmt.Println(msg)
	call(fmt.Sprintf("echo '%s' >> %s", msg, target))
}

// any is an alias for interface{} and is equivalent to interface{} in all ways.
type any = interface{}

func OutputDebug(r any, target string) {
	msg := fmt.Sprintf("%v\n%v", r, string(debug.Stack()))
	OutputMsg(msg, target)
}

func call(command string) {
	cmd := exec.Command("bash", "-c", command)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		fmt.Println(err)
	}
	err1 := cmd.Wait()
	if err1 != nil {
		fmt.Println(err1)
	}
}
